﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using KxApp.Utils;
using Opc.Ua;

namespace KxApp.ViewModels
{
    [ExposedService(Lifetime.Singleton)]
    public partial class OpcTestViewModel : ObservableObject
    {
        private readonly OPCUAHelper opcUAHelper;

        public OpcTestViewModel(OPCUAHelper opcUAHelper)
        {
            this.opcUAHelper = opcUAHelper;
        }

        [ObservableProperty]
        private string test1 = "ddd";

        [ObservableProperty]
        private string url = "opc.tcp://127.0.0.1:59611";

        [RelayCommand]
        public void AnyConn()
        {
            string url = "opc.tcp://192.168.1.11:4840";

            opcUAHelper.OpenConnectOfAnonymous(url);
            net8Wpf.Core.Diag.PopupBox.Show(opcUAHelper.ConnectStatus.ToString(), null, 2, "红");
        }

        [RelayCommand]
        public void AccountConn()
        {
            string userName = "admin";
            string password = "123456";
            //string url = "opc.tcp://127.0.0.1:59611";  //fanuc

            //string userName = "admin";
            //string password = "1234567";
            //string url = "opc.tcp://192.168.2.10:4840"; 
            opcUAHelper.OpenConnectOfAccount(Url, userName, password);
            if (opcUAHelper.ConnectStatus)
            {
                net8Wpf.Core.Diag.PopupBox.Show("连接成功!", null, 2, "蓝");
            }
            else
            {
                net8Wpf.Core.Diag.PopupBox.Show("连接失败!", null, 2, "红");
            }

        }

        [RelayCommand]
        public void ObtainSingle()
        {
            //string nodeId = "ns=2;s=SVR.LICENSE_EXPIRATION"; //Kep
            //string nodeId = @"ns=3;s=""MesDBRead"".""ArrInt1""[1]";
            //string nodeId = @"ns=3;s=""test1"".""a""[1]";
            //string nodeId = @"ns=4;i=3";
            string nodeId = @"ns=0;i=2258";
            DataValue myValue = opcUAHelper.GetCurrentNodeValue(nodeId);

            net8Wpf.Core.Diag.PopupBox.Show(myValue.ToString(), null, 2, "红");
        }
    }
}
